Shader "DistoreEffect"
//面向相机的 扰动
{
	Properties {
		_MainTex ("Base (RGB)", 2D) = "white" {}
		_DistoreTex ("Distore (RGB)", 2D) = "white" {}
		_BaseTex ("_BaseTex (RGB)", 2D) = "white" {}
        _DistoreStrength("_DistoreStrength",range(-0.1,0.1)) = "white" {}
	
    }

	SubShader 
    {



        Tags
        {
            "Queue"="Transparent+1"
            "RenderPipeline"="UniversalPipeline"//urp管线改造
        }
        //映射到uv坐标
        //屏幕是2000*1000
        //位置 200,200 和 700 700 怎么贴？
        //uv坐标是（0.1，0.2）和 （0.35，0.7）
        // GrabPass{} // 抓取屏幕上的图像

		Pass 
        {
            Name "DistorePass"
            Tags 
            { 
                "LightMode"="UniversalForward"
                // "Queue"="Geometry" 
                // "Queue"="Geometry+1"  //保证在所有几何体后面渲染
            }
			HLSLPROGRAM //urp
			#pragma vertex vert
			#pragma fragment frag
			// #include "UnityCG.cginc"
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
			struct v2f {
				float4 pos : SV_POSITION; //屏幕空间（未经齐次坐标除法）
				float2 uv : TEXCOORD0;
                float2 screenUV: TEXCOORD1;
			}

			float4 _MainTex_ST;
			float4 _DistoreTex_ST;
			sampler2D _MainTex;
			sampler2D _DistoreTex;
			sampler2D _BaseTex;

            // sampler2D _GrabTexture;//自动生成的抓屏
            //unity内置，对应的是tilling是xz，offset是zw
            float4 _BaseTex_ST;//xy对贴图进行重复，zw坐标对贴图偏移
            float _DistoreStrength;

            TEXTURE2D(_CameraOpaqueTexture):SAMPLER(sampler_CameraOpaqueTexture);

            float3 BillboardRotate(float3 v)
            {
                //完整版
                //float3 forwardDir = mul(float3x3(UNITY_MATRIX_MV),float4(0,0,1,1)).xyz;
                //float3 rightDir = mul(float3x3(UNITY_MATRIX_MV),float4(0,1,0,1)).xyz;
                //float3 upDir = mul(float3x3(UNITY_MATRIX_MV),float4(1,0,0,1)).xyz;

                float3 forwardDir = UNITY_MATRIX_MV[2].xyz;
                float3 rightDir = UNITY_MATRIX_MV[0].xyz;
                float3 upDir = UNITY_MATRIX_MV[1].xyz;
                float3 localPos = rightDir * v.x + upDir * v.y + forwardDir * v.z;
                return vertex;
            }


			v2f vert(appdata v) {
				v2f o;

                float3 vRot = BillboardRotate(v.vertex);
                //BillboardRotate源码矩阵，把相机的xyz三个向量拿出来


				// o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
				// o.pos = TransformObjectToHClip(v.vertex);
				o.pos = TransformObjectToHClip(vRot);
				// o.pos = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv *  _DistoreTex_ST.xy + _DistoreTex_ST.zw ;//计算uv坐标
                // o.uv = TRANSFORM_TEX(v.uv, _BaseTex);

                //方法2 顶点空间计算抓图的uv坐标，避免像素空间大量运算
                // o.screenUV = ComputeScreenPos(o.pos);//  把裁剪空间转换屏幕空间但是不进行透视除法
                // o.screenUV = ComputeGrabScreenPos(o.pos);//  把裁剪空间转换屏幕空间但是不进行透视除法
                //顶点管线做uv效率高
                // 简化代码
                // o.screenUV.xy = o.pos.xy *float2(0.5,-0.5) + o.pos.w * 0.5;
                // o.screenUV.zw = o.pos.zw;


                o.screenUV = o.pos.xy*float2(0.5,-0.5) + o.pos.w*0.5;
                o.screenUV.zw = o.pos.zw;

                return o;
			}
			half4 frag(v2f i) : SV_Target {

                //调试
				// fixed4 color = tex2D(_MainTex, i.uv);//会有问题
                //return fixed4(i.uv,0,1);

				// float2 uv = i.pos.xy / i.pos.w;//归一化
				
                //这样就做出来啦！！
                float2 screenUV = i.pos.xy / _ScreenParams.xy ;
                
                // 方法1；像素坐标除法
                // float2 uv = i.screenUV.xy / i.screenUV.w
                // fixed4 color = tex2D(_GrabTexture,uv );
			

                fixed4 color = tex2Dproj(_GrabTexture,i.screenUV );// tex2Dproj 内置自动其次坐标
            	return color;

                // float2 uv = i.screenUV.xy / i.screenUV.w * flaot2(0.5,-0.5)+0.5
                // return tex2D(_GrabTexture,uv)
//移到顶点算,减少开销
 
                float2 screenUV  = i.screenUV.xy/i.screenUV.w;
                // return float4(screenUV,0,1); //调试
              
                float2 distoreUV = tex2D(_DistoreTex,i.uv);//能够随时切换序列帧，1234下
                // return float4(distoreUV,0,1); //调试

                // float2 uv = lerp(screenUV,distoreUV,_DistoreStrength * (_CosTime.w*0.5 +0.5))//
                float2 uv = screenUV + distoreUV * distoreUV * _DistoreStrength * _SinTime.w;
                // return float4(uv,0,1); //调试

                // return tex2D(_GrabTexture,uv)
                return SAMPLE_TEXTURE2D(
                    _CameraOpaqueTexture,sampler_CameraOpaqueTexture,uv)

                // return float4(i.uv,0,1); //调试

//原理，将noise的值做为uv坐标
                
    
                //测试是用directx还是opengl的api，起点不同，dx是在左上角
                // #if UNITY_UV_STARTS_AT_TOP
                // return 0;//dx
                // #else 
                // return 1;//opengl
                // #endif
            
            }

			ENDHLSL
		}

	}
}